package com.aaron.guide.proxystudy.jdkProxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @ClassName MyInvocationHandler
 * @description:
 * @author: 荣燊
 * @create: 2021-05-18 15:32
 **/
public class MyInvocationHandler implements InvocationHandler {
    private final Object target;

    public MyInvocationHandler(Object target) {
        this.target = target;
    }

    //参数o:代理对象（class com.sun.proxy.$Proxy0）
    //参数method:需要实现的方法（send）
    //参数objects:实例对象class [Ljava.lang.Object;
    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        //调用方法之前
        System.out.println("before send:"+method.getName());
        System.out.println(method.getDeclaringClass());
        System.out.println(method.getReturnType());
        System.out.println(objects);

        //代理方法调用,参数一：目标对象，参数二：
        Object result = method.invoke(target, objects);

        //调用方法之后
        System.out.println("after send:"+o.getClass());
        return result;
    }
}
